﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Common;
using GtsServer.Model;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// TaskAjax 的摘要说明
    /// </summary>
    public class TaskAjax : IHttpHandler
    {
        TaskController controller = new TaskController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "GetListByPrize":
                        GetListByPrize(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "AddArrayTask":
                        AddArrayTask(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    case "GetListByTaskTypeID":
                        GetListByTaskTypeID(context);
                        break;
                    case "GetTaskName":
                        GetTaskName(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetTaskName(HttpContext context)
        {
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());

            List<Task> list = controller.GetList(typeID);

            List<TaskShow> showList = new List<TaskShow>();
            foreach (var item in list)
                showList.Add(new TaskShow(item));

            for (int i = 0; i < showList.Count; i++)
            {
                showList[i].TaskRemark = Microsoft.JScript.GlobalObject.encodeURIComponent(showList[i].TaskRemark);
            }
            context.Response.Write(JsonHelper.ListToJson<TaskShow>(showList));
        }

        private void AddArrayTask(HttpContext context)
        {
            int taskID = Convert.ToInt32(context.Request["taskID"]);
            int taskNum = Convert.ToInt32(context.Request["taskNum"]);
            bool isSuccess = controller.AddArrayTask(taskID, taskNum);
            context.Response.Write(isSuccess);
        }

        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            context.Response.Write(controller.Delete(id));
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            string name = context.Request["name"];
            string startTime = context.Request["startTime"];
            string endTime = context.Request["endTime"];
            string isRemind = context.Request["isRemind"];
            string IsAllVisual = context.Request["IsAllVisual"];
            int EmptyUnit = Convert.ToInt32(context.Request["EmptyUnit"]);
            string isReachCondition = context.Request["isReachCondition"];
            string content = context.Request["content"];
            string remark = context.Request["remark"];
            string image = context.Request["image"];
            int initGold = Convert.ToInt32(context.Request["initGold"].ToString());
            int sortNum = Convert.ToInt32(context.Request["sortNum"]);
            string IsUseStage = context.Request["IsUseStage"].ToString();

            Task model = new Task()
            {
                ID = id,
                Name = name,
                StartTime = Convert.ToDateTime(startTime),
                EndTime = Convert.ToDateTime(endTime),
                IsRemind = Convert.ToBoolean(isRemind),
                IsAllVisual = Convert.ToBoolean(IsAllVisual),
                EmptyUnit = EmptyUnit,
                IsReachCondition = Convert.ToBoolean(isReachCondition),
                TaskContent = content,
                TaskRemark = remark,
                ImageType = image,
                InitGold = initGold,
                SortNum = sortNum,
                IsUseStage = Convert.ToBoolean(IsUseStage)
            };
            context.Response.Write(controller.Update(model));
        }

        private void Add(HttpContext context)
        {
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());
            string name = context.Request["name"];
            string startTime = context.Request["startTime"];
            string endTime = context.Request["endTime"];
            string isRemind = context.Request["isRemind"];
            string IsAllVisual = context.Request["IsAllVisual"];
            int EmptyUnit = Convert.ToInt32(context.Request["EmptyUnit"]);
            string isReachCondition = context.Request["isReachCondition"];
            string content = context.Request["content"];
            string remark = context.Request["remark"];
            string image = context.Request["image"];
            int sortNum = Convert.ToInt32(context.Request["sortNum"]);
            int initGold = Convert.ToInt32(context.Request["initGold"].ToString());
            string IsUseStage = context.Request["IsUseStage"].ToString();
            Task model = new Task()
            {
                Name = name,
                CrateTime = DateTime.Now,
                StartTime = Convert.ToDateTime(startTime),
                EndTime = Convert.ToDateTime(endTime),
                IsRemind = Convert.ToBoolean(isRemind),
                TaskTypeID = typeID,
                IsAllVisual = Convert.ToBoolean(IsAllVisual),
                EmptyUnit = EmptyUnit,
                IsReachCondition = Convert.ToBoolean(isReachCondition),
                TaskContent = content,
                TaskRemark = remark,
                ImageType = image,
                InitGold = initGold,
                SortNum = sortNum,
                IsUseStage = Convert.ToBoolean(IsUseStage)
            };

            int id = controller.Add(model);
            context.Response.Write(id + "|" + model.CrateTime);
        }

        private void GetList(HttpContext context)
        {
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"].ToString());
            int pageSize = Convert.ToInt32(context.Request["pageSize"].ToString());
            int count = 0;
            List<Task> list = controller.GetList(typeID, pageIndex, pageSize, out count);

            List<TaskShow> showList = new List<TaskShow>();
            foreach (var item in list)
                showList.Add(new TaskShow(item));

            for (int i = 0; i < showList.Count; i++)
            {
                showList[i].TaskRemark = Microsoft.JScript.GlobalObject.encodeURIComponent(showList[i].TaskRemark);
            }
            context.Response.Write(JsonHelper.ListToJson<TaskShow>(showList, count.ToString()));
        }
        private void GetListByPrize(HttpContext context)
        {
            List<Task> list = controller.GetListByPrize();
            context.Response.Write(JsonHelper.ListToJson<Task>(list));
        }
        /// <summary>
        /// 比赛公告调用
        /// </summary>
        /// <param name="context"></param>
        private void GetListByTaskTypeID(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            int taskTypeID = context.Request["taskTypeID"] == null ? 0 : Convert.ToInt32(context.Request["taskTypeID"]);
            string TaskName = context.Request["taskName"] ?? "";
            int count = 0;
            List<Task> list = controller.GetListByTaskTypeID(taskTypeID, TaskName, pageIndex, pageSize, out count);
            string json = JsonHelper.ListToJson<Task>(list, count.ToString());
            context.Response.Write(json);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}